$(document).ready(function(){
	var screenwidth, screenheight, mytop, getPosLeft, getPosTop
	screenwidth = $(window).width();
	screenheight = $(window).height();
	//获取滚动条距顶部的偏移
	mytop = $(document).scrollTop();
	//计算弹出层的left
	getPosLeft = screenwidth / 2 - 260;
	//计算弹出层的top
	getPosTop = screenheight / 2 - 150;
	//css定位弹出层
	$("#intentionCustomBrokerageSet").css({ "left": getPosLeft, "top": getPosTop });
	$("#aSingleRate").css({ "left": getPosLeft, "top": getPosTop });
	//当浏览器窗口大小改变时
	$(window).resize(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 150;
		$("#intentionCustomBrokerageSet").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#aSingleRate").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
	});
	//当拉动滚动条时，弹出层跟着移动
	$(window).scroll(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 150;
		$("#intentionCustomBrokerageSet").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
		$("#aSingleRate").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
	}); 
	 //获取页面文档的高度
	 var docheight = $(document).height();
	//追加一个层，使背景变灰
	$("body").append("<div id='greyBackGround'></div>");
	$("#greyBackGround").css({ "opacity": "0.1", "height": docheight });
	
	
	$("#intentionCustomBrokerageSetBtn").click(function(){
		var addSingleRate = $("#singleRate")[0].value;
		if(addSingleRate==null || addSingleRate==""){
			alert("请输入成单率!");
			return;	
		}
		var reg = /^[+]?(([1-9]\d*[.]?)|(0.))(\d{0,2})?$/;
		if(!reg.test(addSingleRate)){
			alert("请输入正确数值!");
			return;	
		}
		if(addSingleRate>=100){
			alert("请输入正确数值!");
			return;	
		}
		$("#singleRate").val(addSingleRate);
		
		var pushMoney = $("#singlePushMoney")[0].value;
		var pushDivided = $("#pushDivided")[0].value;
		var isUseSingleRateForPushMoney = $("#isUseSingleRateForPushMoney").attr("checked");
		var isUseSingleRateForPushDivided = $("#isUseSingleRateForPushDivided").attr("checked");
		if(pushMoney=="" && pushDivided==""){
			alert("单笔奖金,推送分成,不能都为空,请输入!");
			return;
		}
		var reg = /^[+]?(([1-9]\d*[.]?)|(0.))(\d{0,2})?$/;
		if(pushMoney!=null && pushMoney!=""){
			if(!reg.test(pushMoney)){
				alert("单笔奖金输入有误!");
				return;	
			}
		}
		if(pushDivided!=null && pushDivided!=""){
			if(!reg.test(pushDivided)){
				alert("推送分成输入有误!");
				return;	
			}
			if(pushDivided>=100){
				alert("推送分成输入有误!");
				return;	
			}
		}
		if(isUseSingleRateForPushMoney==false && isUseSingleRateForPushDivided==false){
			alert("必须有一项与成单率挂钩!");
			return;
		}
		if(isUseSingleRateForPushMoney==true && pushMoney==""){
			alert("输入数据有误,请重新输入!");
			return;
		}
		if(pushDivided=="" && isUseSingleRateForPushDivided==true){
			alert("输入数据有误,请重新输入!");
			return;
		}
		if(isUseSingleRateForPushMoney){
			$("#isUseRateForPushMoney").val("1");
		}else{
			$("#isUseRateForPushMoney").val("0");
		}
		if(isUseSingleRateForPushDivided){
			$("#isUseRateForPushDivided").val("1");
		}else{
			$("#isUseRateForPushDivided").val("0");
		}
		$("#brokerageForm").submit();
	});

	$("#bathIntentionCustomBrokerageSetBtn").click(function(){
		var addSingleRate = $("#singleRate")[0].value;
		if(addSingleRate==null || addSingleRate==""){
			alert("请输入成单率!");
			return;	
		}
		var reg = /^[+]?(([1-9]\d*[.]?)|(0.))(\d{0,2})?$/;
		if(!reg.test(addSingleRate)){
			alert("请输入正确数值!");
			return;	
		}
		if(addSingleRate>=100){
			alert("请输入正确数值!");
			return;	
		}
		$("#singleRate").val(addSingleRate);

		var productIdStr = "";
		var list = $("input[id^=bath_][type='checkbox']:checked");
		if(list.length>0){
			for(var int = 0; int < list.length; int++){
				var id = list[int].id.replace("bath_","");
				if(productIdStr==""){
					productIdStr=id;
				}else{
					productIdStr = productIdStr+","+id;
				}	
			}	
		}
		if(productIdStr==""){
			alert("请选择审批项!");
			return;
		}
		var pushMoney = $("#singlePushMoney")[0].value;
		var pushDivided = $("#pushDivided")[0].value;
		var isUseSingleRateForPushMoney = $("#isUseSingleRateForPushMoney").attr("checked");
		var isUseSingleRateForPushDivided = $("#isUseSingleRateForPushDivided").attr("checked");
		if(pushMoney=="" && pushDivided==""){
			alert("单笔奖金,推送分成,不能都为空,请输入!");
			return;
		}
		var reg =  /^[+]?(([1-9]\d*[.]?)|(0.))(\d{0,2})?$/;
		if(pushMoney!=null && pushMoney!=""){
			if(!reg.test(pushMoney)){
				alert("单笔奖金输入有误!");
				return;	
			}
		}
		if(pushDivided!=null && pushDivided!=""){
			if(!reg.test(pushDivided)){
				alert("推送分成输入有误!");
				return;	
			}
			if(pushDivided>=100){
				alert("推送分成输入有误!");
				return;	
			}
		}
		if(isUseSingleRateForPushMoney==false && isUseSingleRateForPushDivided==false){
			alert("必须有一项与成单率挂钩!");
			return;
		}
		if(isUseSingleRateForPushMoney==true && pushMoney==""){
			alert("输入数据有误,请重新输入!");
			return;
		}
		if(pushDivided=="" && isUseSingleRateForPushDivided==true){
			alert("输入数据有误,请重新输入!");
			return;
		}
		if(isUseSingleRateForPushMoney){
			$("#isUseRateForPushMoney").val("1");
		}else{
			$("#isUseRateForPushMoney").val("0");
		}
		if(isUseSingleRateForPushDivided){
			$("#isUseRateForPushDivided").val("1");
		}else{
			$("#isUseRateForPushDivided").val("0");
		}
		$("#productIdStr").val(productIdStr);
		$("#brokerageForm").attr("action", "../../ruleSet/intentionCustomBrokerageSet/bathUpdateBrokerage");
		$("#brokerageForm").submit();	
	});
	
	$("#intentionCustomBrokerageSetBtnCancel").click(function(){
		$("#singlePushMoney")[0].value="";
		$("#pushDivided")[0].value="";
		$("#isUseSingleRateForPushMoney")[0].value="";
		$("#isUseSingleRateForPushDivided")[0].value="";
		 $("#greyBackGround").css("display","none");
		 $("#intentionCustomBrokerageSet").css("display","none");
		 $("#bathIntentionCustomBrokerageSetBtn").css("display","none");
		 $("#intentionCustomBrokerageSetBtn").css("display","block");
		 
	});
	
	$("#aSingleRateBtn").click(function(){
		var addSingleRate = $("#addSingleRate")[0].value;
		if(addSingleRate==null || addSingleRate==""){
			alert("请输入成单率!");
			return;	
		}
		var reg = /^[+]?(([1-9]\d*[.]?)|(0.))(\d{0,2})?$/;
		if(!reg.test(addSingleRate)){
			alert("请输入正确数值!");
			return;	
		}
		if(addSingleRate>=100){
			alert("请输入正确数值!");
			return;	
		}
		$("#singleRate").val(addSingleRate);
		$("#singleRateForm").submit();
	});
	$("#aSingleRateBtnCancel").click(function(){
		 $("#greyBackGround").css("display","none");
		 $("#aSingleRate").css("display","none");
	});
	
});
function intentionCustomBrokerageSet(productId){
	$("#productId").val(productId);
	$("#singlePushMoney")[0].value="";
	$("#pushDivided")[0].value="";
	$("#intentionCustomBrokerageSetBtn").css("display","block");
	$("#bathIntentionCustomBrokerageSetBtn").css("display","none");
	$("#isUseSingleRateForPushMoney").attr("checked",false);
	$("#isUseSingleRateForPushDivided").attr("checked",false);
	$("#greyBackGround").css("display","block");
	$("#intentionCustomBrokerageSet").fadeIn("slow");
}

function bathIntentionCustomBrokerageSet(){
	var productIdStr = "";
	var list = $("input[id^=bath_][type='checkbox']:checked");
	if(list.length>0){
		for(var int = 0; int < list.length; int++){
			var id = list[int].id.replace("bath_","");
			if(productIdStr==""){
				productIdStr=id;
			}else{
				productIdStr = productIdStr+","+id;
			}	
		}	
	}
	if(productIdStr==""){
		alert("请选择审批项!");
		return;
	}
	 $("#intentionCustomBrokerageSetBtn").css("display","none");
	 $("#bathIntentionCustomBrokerageSetBtn").css("display","block");
	 $("#greyBackGround").css("display","block");
	 $("#intentionCustomBrokerageSet").fadeIn("slow");
}
function updateASingleRate(){
	 $("#greyBackGround").css("display","block");
	 $("#aSingleRate").fadeIn("slow");
}